package cn.tedu.hospitalgroupproject.server.content.service.impl;

import cn.tedu.hospitalgroupproject.server.common.ex.ServiceException;
import cn.tedu.hospitalgroupproject.server.common.pojo.vo.PageData;
import cn.tedu.hospitalgroupproject.server.common.web.ServiceCode;
import cn.tedu.hospitalgroupproject.server.content.dao.persist.repository.IAppointmentRepository;
import cn.tedu.hospitalgroupproject.server.content.dao.persist.repository.IDoctorRepository;
import cn.tedu.hospitalgroupproject.server.content.dao.persist.repository.IDoctorTitleAppLimitRepository;
import cn.tedu.hospitalgroupproject.server.content.dao.persist.repository.impl.DoctorTitleAppLimitRepositoryImpl;
import cn.tedu.hospitalgroupproject.server.content.pojo.entity.Doctor;
import cn.tedu.hospitalgroupproject.server.content.pojo.entity.DoctorTitleAppLimit;
import cn.tedu.hospitalgroupproject.server.content.pojo.param.DoctorTitleAppLimitAddNewParam;
import cn.tedu.hospitalgroupproject.server.content.pojo.param.DoctorTitleAppLimitUpdateParam;
import cn.tedu.hospitalgroupproject.server.content.pojo.vo.DoctorStandardVO;
import cn.tedu.hospitalgroupproject.server.content.pojo.vo.DoctorTitleAppLimitListVO;
import cn.tedu.hospitalgroupproject.server.content.pojo.vo.DoctorTitleAppLimitStandardVO;
import cn.tedu.hospitalgroupproject.server.content.service.IDoctorTitleAppLimitService;
import com.sun.xml.internal.ws.developer.Serialization;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


/**
 * 处理医生职称级别预约挂号上限数据的业务实现类
 *
 * @author java@tedu.cn
 * @version 1.0
 */
@Service
@Slf4j
public class DoctorTitleAppLimitServiceImpl implements IDoctorTitleAppLimitService {

    @Value("${hospital-appointment.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;

    @Autowired
    private IDoctorTitleAppLimitRepository doctorTitleAppLimitRepository;

    @Autowired
    private IDoctorRepository doctorRepository;

    public DoctorTitleAppLimitServiceImpl() {
        log.info("创建业务对象:DoctorTitleAppLimitServiceImpl");
    }

    @Override
    public void addNew(DoctorTitleAppLimitAddNewParam doctorTitleAppLimitAddNewParam) {
        log.debug("开始处理[新增医生职称级别预约挂号上限]的业务,参数:{}",doctorTitleAppLimitAddNewParam);

        DoctorTitleAppLimitStandardVO currentVO
                =doctorTitleAppLimitRepository.selectByTitleName(
                        doctorTitleAppLimitAddNewParam.getDoctorTitle());
        if(currentVO!=null){//当已有相同职称时
            String message="新增医生职称数据失败,尝试添加的医生职称已存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }

        DoctorTitleAppLimit doctorTitleAppLimit=new DoctorTitleAppLimit();
        BeanUtils.copyProperties(doctorTitleAppLimitAddNewParam,doctorTitleAppLimit);

        doctorTitleAppLimitRepository.insert(doctorTitleAppLimit);
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理[删除指定职称ID下的数据]业务,医生ID:{}",id);
        int num= doctorRepository.countByTitleId(id);
        if(num>0){
            String message="删除指定职称ID下数据失败,当前职称下还存在医生数据!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        doctorTitleAppLimitRepository.deleteById(id);
    }

    @Override
    public void updateInfoById(DoctorTitleAppLimitUpdateParam doctorTitleAppLimitUpdateParam) {
        log.debug("开始处理【修改职称预约信息】的业务，参数：{}",doctorTitleAppLimitUpdateParam);

        Long id = doctorTitleAppLimitUpdateParam.getDoctorTitleId();
        DoctorTitleAppLimitStandardVO currentVO = doctorTitleAppLimitRepository.getStandardById(id);
        if (currentVO == null ) {
            String message = "修改职称信息失败，尝试修改的职称数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        DoctorTitleAppLimit doctorTitleAppLimit=new DoctorTitleAppLimit();
        BeanUtils.copyProperties(doctorTitleAppLimitUpdateParam, doctorTitleAppLimit);

        int rows=doctorTitleAppLimitRepository.updateById(doctorTitleAppLimit);
        if (rows != 1) {
            String message = "修改职称信息失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public DoctorTitleAppLimitStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询职称】业务，参数：{}", id);
        DoctorTitleAppLimitStandardVO currentVO= doctorTitleAppLimitRepository.getStandardById(id);
        if(currentVO==null){
            String message="获取ID下职称数据失败,尝试访问的职称数据不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        return currentVO;
    }

    @Override
    public PageData<DoctorTitleAppLimitListVO> list(Integer pageNum) {
        log.debug("开始处理【查询医生职称预约上限列表】业务，页码：{}", pageNum);
        PageData<DoctorTitleAppLimitListVO> pageData = doctorTitleAppLimitRepository.list(pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<DoctorTitleAppLimitListVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询医生职称预约上限列表】业务，页码：{}", pageNum);
        PageData<DoctorTitleAppLimitListVO> pageData = doctorTitleAppLimitRepository.list(pageNum, pageSize);
        return pageData;
    }
}
